package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.WaiterTypeDao;
import com.baichang.taoshunda.entity.WaiterType;
import com.baichang.taoshunda.service.WaiterService;
import com.baichang.taoshunda.service.WaiterTypeService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/18 0018.
 */
@Service
public class WaiterTypeServiceImpl implements WaiterTypeService {
    @Autowired
    private WaiterTypeDao waiterTypeDao;
    @Autowired
    private WaiterService waiterService;

    /**
     * 接口查询本地服务
     *
     * @param isHome 是否首页
     * @return
     */
    @Override
    public List<Map<String, Object>> getwaiterType(int isHome) {
        return waiterTypeDao.getwaiterType(isHome);
    }

    /**
     * 查询本地服务分类
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryLifeType(String name, int nowPage, int pageSize) {
        return waiterTypeDao.queryLifeType(name, nowPage, pageSize);
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateLifeState(Integer id, Integer state) throws ServiceException {
        int waiterCount = waiterService.getWaiterCount(id);
        if (waiterCount > 0) {
            throw new ServiceException("该分类下存在本地服务商家，暂不支持删除功能！");
        }
        return waiterTypeDao.updateLifeState(id, state);
    }

    /**
     * 添加本地服务列表
     *
     * @param
     * @param isSpecial
     * @param name
     * @param pic
     * @return
     */
    @Override
    public long addWaiterLifeType(Integer isSpecial, Integer sorting, String name, String pic, BigDecimal price) throws ServiceException {
        List<Map<String, Object>> list = waiterTypeDao.getTypeName();
        for (int i = 0; i < list.size(); i++) {
            String names = list.get(i).get("name").toString();
            if (names.equals(name)) {
                throw new ServiceException("请不要重复添加分类名称");
            }
        }
        WaiterType waiterType = new WaiterType();
        waiterType.setIsHome(0);
        waiterType.setIsSpecial(isSpecial);
        waiterType.setSorting(sorting);
        waiterType.setWaierPic(pic);
        waiterType.setWaiterName(name);
        waiterType.setState(1);
        waiterType.setCreated(new Date());
        waiterType.setPrice(price);
        return waiterTypeDao.save(waiterType);
    }

    /**
     * 修改本地服务列表
     *
     * @param id
     * @param sorting
     * @param isSpecial
     * @param name
     * @param pic
     * @param price
     * @return
     */
    @Override
    public int updateWaiterLifeType(Integer id, Integer sorting, Integer isSpecial, String name, String pic, String price) {
        if (isSpecial != -1) {
            Map map = new HashedMap();
            map.put("isSpecial", isSpecial);
            map.put("waiterName", name);
            map.put("waierPic", pic);
            map.put("price", price);
            return waiterTypeDao.update(map, id);
        } else {
            return waiterTypeDao.updateSorting(id, sorting);
        }
    }

    /**
     * 查询本地服务商家列表
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryWaiterTypeId() {
        return waiterTypeDao.queryWaiterTypeId();
    }

    /**
     * 首页推荐
     *
     * @param id
     * @param isHome
     * @return
     */
    @Override
    public int updateIsHome(Integer id, Integer isHome) {
        return waiterTypeDao.updateIsHome(id, isHome);
    }

    /**
     * 查询服务分类
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryWaiterType() {
        return waiterTypeDao.queryWaiterType();
    }
}
